package in.abip.chisame;

import in.abip.chisame.model.DatabaseHelper;

import java.io.IOException;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.database.SQLException;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TextView;

/**
 * Main tab activity.
 * 
 * @author Julot.
 * @since Thursday, January 26th, 2011.
 */
public class MainTabActivity extends TabActivity {
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		createDatabase();
		setContentView(R.layout.main_tab_activity);
		changeTitle();
		
		Resources res = getResources(); // Resource object to get Drawables
		TabHost tabHost = getTabHost();  // The activity TabHost
		TabHost.TabSpec spec;  // Resusable TabSpec for each tab
		Intent intent;  // Reusable Intent for each tab
		
		// Create an Intent to launch an Activity for the tab (to be reused)
		intent = new Intent().setClass(this, RoomListActivity.class);
		
		// Initialize a TabSpec for each tab and add it to the TabHost
		spec = tabHost.newTabSpec("room");
		spec.setIndicator(
			res.getString(R.string.room),
			res.getDrawable(R.drawable.ic_tab_room)
		);
		spec.setContent(intent);
		tabHost.addTab(spec);
		
		// Do the same for the other tabs
		intent = new Intent().setClass(this, LightListActivity.class);
		spec = tabHost.newTabSpec("light");
		spec.setIndicator(
			res.getString(R.string.light),
			res.getDrawable(R.drawable.ic_tab_light)
		);
		spec.setContent(intent);
		tabHost.addTab(spec);
		
		intent = new Intent().setClass(this, HvacListActivity.class);
		spec = tabHost.newTabSpec("hvac");
		spec.setIndicator(
			res.getString(R.string.hvac),
			res.getDrawable(R.drawable.ic_tab_hvac)
		);
		spec.setContent(intent);
		tabHost.addTab(spec);
		
		intent = new Intent().setClass(this, RemoteListActivity.class);
		spec = tabHost.newTabSpec("remote");
		spec.setIndicator(
			res.getString(R.string.remote),
			res.getDrawable(R.drawable.ic_tab_remote)
		);
		spec.setContent(intent);
		tabHost.addTab(spec);
		
		intent = new Intent().setClass(this, CameraListActivity.class);
		spec = tabHost.newTabSpec("camera");
		spec.setIndicator(
			res.getString(R.string.camera),
			res.getDrawable(R.drawable.ic_tab_camera)
		);
		spec.setContent(intent);
		tabHost.addTab(spec);
		
		intent = new Intent().setClass(this, DoorListActivity.class);
		spec = tabHost.newTabSpec("door");
		spec.setIndicator(
			res.getString(R.string.door),
			res.getDrawable(R.drawable.ic_tab_door)
		);
		spec.setContent(intent);
		tabHost.addTab(spec);
		
		intent = new Intent().setClass(this, TentacleListActivity.class);
		spec = tabHost.newTabSpec("tentacle");
		spec.setIndicator(
			res.getString(R.string.tentacle),
			res.getDrawable(R.drawable.ic_tab_simulation)
		);
		spec.setContent(intent);
		tabHost.addTab(spec);
		
//		intent = new Intent().setClass(this, PlaygroundActivity.class);
//		spec = tabHost.newTabSpec("playground");
//		spec.setIndicator(
//			res.getString(R.string.playground),
//			res.getDrawable(R.drawable.ic_tab_simulation)
//		);
//		spec.setContent(intent);
//		tabHost.addTab(spec);
//		tabHost.setCurrentTab(7);
		
		/*
		TabWidget tabWidget = getTabWidget();
		for (int i = 0; i < tabWidget.getChildCount(); i++) {
            View v = tabWidget.getChildAt(i);
            v.setBackgroundColor(Color.parseColor("#FF0000"));
        }
        */ 
	}
	
	/** Change title */
	protected void changeTitle() {
		TextView text = (TextView) findViewById(R.id.title);
		text.setText(getString(R.string.title));
	}
	
    /** Create database if not exists. */
    protected void createDatabase() {
        DatabaseHelper dbHelper = new DatabaseHelper(this);
        try {
            dbHelper.createDataBase();
        }
        catch (IOException ioe) {
            throw new Error("Unable to create database");
        }
        try {
            dbHelper.openDataBase();
            dbHelper.close();
        }
        catch(SQLException sqle){
            throw sqle;
        }
    }
}
